package si.yun.utils;

import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import si.yun.dao.HomeDao;
import si.yun.dao.MicroCircleDao;
import si.yun.entity.SyCarousel;
import si.yun.service.PersonalService;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @program: ThinkManage
 * @description: Redis缓存数据同步
 * @author: 崔晓晨
 * @create: 2021-12-25 12:27
 **/
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class DynamicScheduleTask implements SchedulingConfigurer {
    //每天的凌晨0点执行
    //private String cron = "0 0 0 * * ?";
    //每1小时执行一次
    private String cron = "0 0 */1 * * ?";
    //private String cron = "0/6 * * * * ?";
    @Autowired
    private HomeDao homeDao;
    @Autowired
    private MicroCircleDao microCircleDao;
    @Autowired
    private PersonalService personalService;
    /*获取服务器路径*/
    @Value("${path1}")
    private String path;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 执行定时任务.
     */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(
                //1.添加任务内容(Runnable)
                () -> System.out.println("每日Redis同步数据库数据: " + LocalDateTime.now().toLocalTime()),
                //2.设置执行周期(Trigger)
                triggerContext -> {
                    //3 合法性校验.
                    if (StringUtils.isEmpty(cron)) {
                        // Omitted Code ..
                    }
                    String endDate = String.valueOf(LocalDateTime.now().toLocalDate());
                    //同步redis的数据
                    updCarouselInfo();
                    updCourseInfo();
                    updForumClass();
                    updMicrocircleDetailsList();
                    updMicroMall();
                    return new CronTrigger(cron).nextExecutionTime(triggerContext);
                }
        );
    }
    /**
     * @Description  同步微圈分类
     * @return si.yun.utils.JsonData
     * @Date 2021/12/25 12:29
     **/
    private void updForumClass() {
        List<Map<String, Object>> ForumClass = microCircleDao.selForumClass();
        //redis缓存
        ValueOperations<String,String> vo = redisTemplate.opsForValue();
        Gson gson = new Gson();
        vo.set("selForumClass", gson.toJson(ForumClass));
    }
    /**
     * @Description  同步微圈分类，各分类的数据
     * @return si.yun.utils.JsonData
     * @Date 2021/12/25 12:29
     **/
    private void updMicrocircleDetailsList() {
        //redis缓存
        ValueOperations<String,String> vo = redisTemplate.opsForValue();
        String value = vo.get("selForumClass");
        Gson gson = new Gson();
        List<Map<String , Object>> ForumClass = gson.fromJson(value, List.class);

        System.out.println(ForumClass);
        for(Map<String ,Object> i:ForumClass){
            int id =  Double.valueOf((Double) i.get("id")).intValue();
            List<Map<String, Object>> selForumClass = microCircleDao.selForum(id);
            vo.set("updMicrocircleDetailsList"+String.valueOf(id), gson.toJson(selForumClass));
        }
    }
    /**
     * @Description  同步轮播图数据
     * @return si.yun.utils.JsonData
     * @Date 2021/12/25 12:29
     **/
    private void updCarouselInfo() {
        List<SyCarousel> syCarousels = homeDao.selCarouselInfo();
        for(SyCarousel list:syCarousels){
            list.setImgAddress(path+list.getImgAddress());
            if(list.getConnect()==null){
                list.setConnect("");
            }else{
                list.setConnect(path+list.getConnect());
            }
        }
        //redis缓存
        ValueOperations<String,String> vo = redisTemplate.opsForValue();
        Gson gson = new Gson();
        vo.set("carousels", gson.toJson(syCarousels));
    }
    /**
     * @Description  更新redis的课程数据缓存
     * @return void 
     * @Date 2021/12/25 12:39
     **/
    private  void updCourseInfo(){
        List<Map<String, Object>> list = homeDao.selCourseInfo();
        List<Map<String, Object>> list1 = homeDao.selCourseInfoBefore();
        for(Map<String, Object> ma:list1){
            list.add(ma);
        }
        int i = 0;
        for(Map<String, Object> map:list){
            Object course_price = map.get("course_price");
            Object course_nprice = map.get("course_nprice");
            if(course_price==null){
                map.put("course_price","免费");
            }
            if(course_nprice==null){
                map.put("course_nprice","免费");
            }
            map.put("url",path+map.get("url"));
            String trueName = (String) map.get("true_name");
            String true_name = trueName.substring(0, 1);
            map.put("true_name",true_name+"老师");
            i++;
        }
        //redis缓存
        ValueOperations<String,String> vo = redisTemplate.opsForValue();
        Gson gson = new Gson();
        vo.set("courseInfo", gson.toJson(list));
    }
    /**
     * @Description  同步微商城
     * @return si.yun.utils.JsonData
     * @Date 2021/12/25 12:29
     **/
    private void updMicroMall() {
        List<Map<String, Object>> ForumClass = personalService.selMicroMall();
        //redis缓存
        ValueOperations<String,String> vo = redisTemplate.opsForValue();
        Gson gson = new Gson();
        vo.set("selMicroMall", gson.toJson(ForumClass));
    }

}
